package com.h.cheng.pselector.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.h.cheng.pselector.R;
import com.h.cheng.pselector.base.BaseMediaLoader;
import com.h.cheng.pselector.base.BaseRecAdapter;
import com.h.cheng.pselector.base.BaseRecViewHolder;
import com.h.cheng.pselector.base.load.PSManager;
import com.h.cheng.pselector.bean.BaseMedia;
import com.h.cheng.pselector.view.SpaceDecoration;

import java.util.ArrayList;
import java.util.List;

import static com.h.cheng.pselector.base.load.PSConfig.IMG_CONTENT;

public class VideoSelectorActivity extends AppCompatActivity {

    private RelativeLayout back;
    private TextView tv_simple_title;
    private RecyclerView rv_select;
    private Context context;
    private ArrayList<BaseMedia> selectMedia = new ArrayList<>();
    private TextView tv_select;
    private RelativeLayout rl_select;
    private int spanCount;
    private int maxCount;
    private RelativeLayout rl_title;
    private SelectAdapter adapter;
    private int type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = this;

        setContentView(R.layout.activity_img_selector);
        initView();
        initData();
    }

    private void initData() {
        PSManager.getInstance().loadVideoMedia(getContentResolver(), "", 0, new PSManager.IMediaCallBack<BaseMedia>() {
            @Override
            public void postMedia(@Nullable List<BaseMedia> medias, int count) {
                adapter.setNewData(medias);
            }
        });

    }

    private void initView() {
        back = findViewById(R.id.back);
        tv_simple_title = findViewById(R.id.tv_simple_title);
        rv_select = findViewById(R.id.rv_select);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        rl_select = findViewById(R.id.rl_select);
        rl_title = findViewById(R.id.rl_title);
        tv_select = findViewById(R.id.tv_select);
        tv_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putParcelableArrayListExtra(IMG_CONTENT, selectMedia);
                setResult(RESULT_OK, intent);
                finish();
            }
        });


        spanCount = PSManager.getInstance().getmConfig().getSpanCount();
        maxCount = PSManager.getInstance().getmConfig().getmMaxCount();

        String title = PSManager.getInstance().getmConfig().getTitle();
        int titleColor = PSManager.getInstance().getmConfig().getTitleColor();
        int titlebgColor = PSManager.getInstance().getmConfig().getTitleBgColor();

        tv_simple_title.setText(title == null ? "" : title);
        tv_simple_title.setTextColor(getResources().getColor(titleColor));
        rl_title.setBackgroundColor(getResources().getColor(titlebgColor));


        adapter = new SelectAdapter(null);
        rv_select.setLayoutManager(new GridLayoutManager(context, spanCount));
        rv_select.addItemDecoration(new SpaceDecoration(5, spanCount));
        rv_select.setAdapter(adapter);
    }


    public class SelectAdapter extends BaseRecAdapter<BaseMedia, ImgViewHolder> {

        public SelectAdapter(List list) {
            super(list);
        }


        @Override
        public void onHolder(final ImgViewHolder holder, final BaseMedia bean, int position) {

            if (bean.isSelected()) {
                holder.cb_select.setImageResource(R.mipmap.ic_select);
            } else {
                holder.cb_select.setImageResource(R.mipmap.ic_unselect);
            }

            BaseMediaLoader.getInstance().displayIMG(holder.iv_select, bean.getPath(), 180, 180);

            holder.cb_select.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (bean.isSelected()) {
                        bean.setSelected(false);
                        holder.cb_select.setImageResource(R.mipmap.ic_unselect);
                    } else {
                        if (selectMedia.size() >= maxCount) {
                            showToast("最多只能选" + maxCount + "张");
                            return;
                        }
                        bean.setSelected(true);
                        holder.cb_select.setImageResource(R.mipmap.ic_select);
                    }

                    if (selectMedia.contains(bean)) {
                        selectMedia.remove(bean);
                    } else {
                        selectMedia.add(bean);
                    }
                    initUi();
                }
            });
            holder.iv_select.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
        }

        @Override
        public ImgViewHolder onCreateHolder() {
            return new ImgViewHolder(getViewByRes(R.layout.item_select_normal));
        }

    }

    private void initUi() {
        if (selectMedia.size() > 0) {
            rl_select.setVisibility(View.VISIBLE);
        } else {
            rl_select.setVisibility(View.GONE);
        }
        tv_select.setText("确定 " + selectMedia.size() + " / " + maxCount);
    }

    public class ImgViewHolder extends BaseRecViewHolder {
        public View rootView;
        public ImageView iv_select;
        public ImageView cb_select;

        public ImgViewHolder(View rootView) {
            super(rootView);
            this.rootView = rootView;
            this.iv_select = rootView.findViewById(R.id.iv_select);
            this.cb_select = rootView.findViewById(R.id.cb_select);
        }

    }

    private void showToast(String str) {
        Toast.makeText(context, str, Toast.LENGTH_LONG).show();
    }

}
